<template>
  <div>
    <!-- 大客户订单提报 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      columnsRef="zjPickAddress"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" @click="addHandle()" :disabled="isOpen">批量调整</multiuse-button>
      </template>
    </FilterTable>

    <BaseDialog :visible.sync="isowner" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :rowData="formData" @close="emitcloseHandler" />
    </BaseDialog>

    <BaseDialog :visible.sync="isProject" :title="'选择项目'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <AccountProject :dealerCode="dealerCode" @close="ProjectClose" />
    </BaseDialog>

    <Drawer :visible.sync="detailVisible" :title="'大客户订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <detailModel :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
    <BaseDialog :visible.sync="isAdjustment" :title="'批量调整'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <BatchAdjustment :list="selectList" @close="AdjustmentClose" />
    </BaseDialog>
  </div>
</template>
<script>
import { getList, getTypesub, vehicleConfiguration, gettmFiAccountType, gettmVsWarehouse } from '@/api/omSale/KeyCustomers/PickAddress/index.js';
import { dictionary } from '@/utils/dictMixin';
import detailModel from './detailModel';
import AccountProject from './AccountProject';
import ownerSeacher from './ownerSeacher';
import BatchAdjustment from './BatchAdjustment';
export default {
  name: 'TemplateDistribution',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {},
      fetchApi: getList,
      formData: {},
      detailVisible: false,
      isProject: false,
      ProjectId: '',
      isDiscount: false,
      isowner: false,
      isAdjustment: false,
      dealerCode: '',
      isOpen: true,
      selectList: []
    };
  },
  components: {
    ownerSeacher,
    detailModel,
    AccountProject,
    BatchAdjustment
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getvehicleList();
    this.gettmFiAccountType();
    this.gettmVsWarehouse();
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请选择',
          readonly: true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          },
          change: p => {
            if (!p) {
              this.dealerCode = '';
            }
          }
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList: [
            { text: '直销大客户订单', value: 60061002 },
            { text: '区域大客户订单', value: 60061003 },
            { text: '代交车大客户订单', value: 60061004 }
          ],
          change: p => {
            if (p) {
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').itemList = [];
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').initialValue = '';
              this.getNotPage(p);
            } else {
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').itemList = [];
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').initialValue = '';
            }
          }
        },
        {
          type: 'SELECT',
          label: '订单细分类型',
          fieldName: 'orderDetailType',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'SELECT',
          label: '工厂仓库',
          fieldName: 'parentWarehouseId',
          placeholder: '请选择',
          itemList: []
        },

        {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'brandProductList',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装', '颜色'] },
          itemList: []
        },
        {
          type: 'INPUT',
          label: '销售订单编号',
          fieldName: 'doNo',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '资金类型',
          fieldName: 'accountType',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '大客户项目名称',
          fieldName: 'majorProjectName',
          placeholder: '请选择',
          readonly: true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
              </div>
            );
          }
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  详情
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '事业部',
          dataIndex: 'deptName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '办事处',
          dataIndex: 'officeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订货单编号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '销售订单号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '收车地址',
          dataIndex: 'address',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6006'),
          render: p => {
            return <span>{this.createDictText(p.row.orderType, '6006')}</span>;
          }
        },
        {
          title: '订单细分类型',
          dataIndex: 'orderDetailTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '订单状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6002'),
          render: p => {
            return <span>{this.createDictText(p.row.orderStatus, '6002')}</span>;
          }
        },
        {
          title: '资金类型',
          dataIndex: 'accountType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editType: 'select',
          editItems: []
        },
        {
          title: '大客户合同名称',
          dataIndex: 'majorContractName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '大客户项目编号',
          dataIndex: 'dkhuxmbh',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '大客户项目名称',
          dataIndex: 'majorProjectName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '大客户类型',
          dataIndex: 'ddzje',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单年月',
          dataIndex: 'orderYearMonth',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '联系人',
          dataIndex: 'linkMan',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系电话',
          dataIndex: 'linkPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '要求交付日期',
          dataIndex: 'requiredDeliveryDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装',
          dataIndex: 'optionName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '公告号',
          dataIndex: 'noticeNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '工厂仓库',
          dataIndex: 'parentWarehouse',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '仓库名称',
          dataIndex: 'dlrWarehouseId',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '分配数量',
          dataIndex: 'matchCount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '市场指导价',
          dataIndex: 'singleAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '国补金额',
          dataIndex: 'subsidyAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '折扣金额',
          dataIndex: 'discountAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '折扣率(%)',
          dataIndex: 'discount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否使用返利',
          dataIndex: 'isRebate',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: p => {
            return <span>{this.createDictText(p.row.isRebate, '1004')}</span>;
          }
        },
        {
          title: '返利使用金额',
          dataIndex: 'rebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '单车返利金额',
          dataIndex: 'singleRebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '建店返利金额',
          dataIndex: 'buildRebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '折后单价',
          dataIndex: 'afterDiscountAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '账户可用余额',
          dataIndex: 'availableAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '运输方式',
          dataIndex: 'transportType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6004'),
          render: p => {
            return <span>{this.createDictText(p.row.transportType, '6004')}</span>;
          }
        },
        {
          title: '入库日期',
          dataIndex: 'receiveDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '提报时间',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '审核人',
          dataIndex: 'checkPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '审核时间',
          dataIndex: 'checkDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '分配时间',
          dataIndex: 'matchDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '订单确认时间',
          dataIndex: 'confirmDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '财务审核时间',
          dataIndex: 'financeCheckDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: 'sap出库时间',
          dataIndex: 'sapOutDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '金税发票号',
          dataIndex: 'goldenInvoiceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '开票日期',
          dataIndex: 'invoiceDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '三方票号',
          dataIndex: 'billNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    selectChangeHandle(row) {},
    seacherFun(type) {
      if (type == 1) {
        this.issearch = true;
      } else {
        this.isProject = true;
      }
    },
    addHandle(data) {
      this.isAdjustment = true;
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data) {
      this.formData = data;
      this.detailVisible = true;
    },
    selectChangeHandle(val) {
      if (val.length > 0) {
        this.selectList = val;
        this.isOpen = false;
      } else {
        this.selectList = [];
        this.isOpen = true;
      }
    },
    emitcloseHandler() {
      this.visible = false;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    ProjectClose(val) {
      if (val) {
        this.topFilterList.map(x => {
          if (x.fieldName == 'majorProjectName') {
            x.initialValue = val.projectName;
            this.ProjectId = val.projectCode;
          }
        });
      }
      this.isProject = false;
    },
    async getNotPage(orderType) {
      let list = await getTypesub({ orderType: orderType });
      this.topFilterList.map(x => {
        if (x.fieldName == 'orderDetailType') {
          x.itemList = list.data.map(x => ({ text: x.subName, value: x.id }));
        }
      });
    },
    async gettmFiAccountType() {
      let res = (await gettmFiAccountType({})).data.map(x => ({ text: x.accountTypeName, value: x.id }));
      this.columns.find(x => x.dataIndex == 'accountType').editItems = res;
      this.columns.find(x => x.dataIndex == 'accountType').filterItems = res;
      this.topFilterList.find(x => x.fieldName == 'accountType').itemList = res;
    },
    async gettmVsWarehouse() {
      let res = (await gettmVsWarehouse({})).data.map(x => ({ text: x.warehouseName, value: x.id }));
      this.topFilterList.find(x => x.fieldName == 'parentWarehouseId').itemList = res;
    },
    // 获取车型车系
    async getvehicleList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'brandProductList').itemList = res.data;
    },
    AdjustmentClose(val) {
      if (val) {
        this.fetchParams.dealerCode = this.dealerCode;
        this.fetchParams.projectCode = this.ProjectId;
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
      this.isAdjustment = false;
    },
    resetChange(val) {
      this.dealerCode = '';
      this.fetchParams.dealerCode = '';
      this.fetchParams.projectCode = '';
      this.ProjectId = '';
      this.fetchParams = Object.assign({}, this.fetchParams);
    }
  }
};
</script>
